<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns" :dataSource="dataSource"
        :pagination="ipagination" :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap" @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="getChildrenList(record.id)">下级标签</a>
          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a v-if="record.status == 1" @click="close(record)">关闭</a>
          <a v-else @click="open(record)">开启</a>
        </span>

      </a-table>
    </div>

    <a-modal v-model="visible" title="二级标签" @ok="handleOk">
      <a-button @click="handleAddChildren(pid)" type="primary" icon="plus">新增</a-button>
      <a-table :columns="childrenColumns" :data-source="childrenList">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEditChildren(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDeleteChildren(record)">删除</a>
        </span>
      </a-table>
    </a-modal>
    <label-topic-modal ref="modalForm" @ok="modalFormOk"></label-topic-modal>

    <a-modal v-model="childrenVisible" title="二级标签" @ok="handleOk">
      <a-form-model :model="childrenFrom">
        <!--              <a-form-model-item label="ID">
                 <a-input v-model="childrenFrom.id"></a-input>
              </a-form-model-item>
              
              <a-form-model-item label="pid">
                 <a-input v-model="childrenFrom.pid"></a-input>
              </a-form-model-item> -->
        <a-form-model-item label="标签名称">
          <a-input v-model="childrenFrom.name"></a-input>
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="saveOrUpdate">
            保存
          </a-button>
        </a-form-model-item>

      </a-form-model>

    </a-modal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import {
    mixinDevice
  } from '@/utils/mixin'
  import {
    JeecgListMixin
  } from '@/mixins/JeecgListMixin'
  import LabelTopicModal from './modules/LabelTopicModal'


  export default {
    name: "LabelTopicList",
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      LabelTopicModal
    },
    data() {
      return {
        childrenFrom: {
          id: '',
          pid: '',
          name: ''
        },
        pid: '',
        childrenVisible: false,
        visible: false,
        description: '标签话题表管理页面',
        // 表头
        columns: [{
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function(t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '标签名称',
            align: "center",
            dataIndex: 'labelTopicName'
          },
          {
            title: '开启/关闭',
            align: "center",
            dataIndex: 'status_dictText'
          },
          // {
          //   title: '已删除/未删除',
          //   align: "center",
          //   dataIndex: 'delFlag_dictText'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: "right",
            width: 147,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        childrenColumns: [{
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function(t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '标签名称',
            align: "center",
            dataIndex: 'name'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: "right",
            width: 147,
            scopedSlots: {
              customRender: 'action'
            }
          }
        ],
        url: {
          list: "/labelTopic/labelTopic/list",
          delete: "/labelTopic/labelTopic/delete",
          deleteBatch: "/labelTopic/labelTopic/deleteBatch",
          exportXlsUrl: "/labelTopic/labelTopic/exportXls",
          importExcelUrl: "labelTopic/labelTopic/importExcel",

        },
        dictOptions: {},
      }
    },
    created() {},
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig() {},
      getChildrenList(id) {
        this.axios.get("/labelTopicChildren/labelTopicChildren/listByPid", {
          params: {
            pid: id,
            labelName: ''
          }
        }).then((res) => {
          this.childrenList = res.result;
          this.pid = id;
          this.visible = true;
        })
      },
      open(record) {
        //状态：1启用，2停用
        this.axios.get("/labelTopic/labelTopic/openOrClose", {
          params: {
            id: record.id,
            status: '1'
          }
        }).then((res) => {
          this.loadData();
        })
      },
      close(record) {
        //状态：1启用，2停用
        this.axios.get("/labelTopic/labelTopic/openOrClose", {
          params: {
            id: record.id,
            status: '2'
          }
        }).then((res) => {
          this.loadData();
        })
      },
      handleOk(e) {
        console.log(e);
        this.pid = '';
        this.childrenList = [];
        this.visible = false;
      },
      handleAddChildren(pid) {
        //打开对话框
        this.childrenFrom.pid = pid;
        this.childrenVisible = true;
      },
      handleEditChildren(record) {
        // alert(record.name);
        this.childrenFrom = record;
        this.childrenVisible = true;
      },
      saveOrUpdate(){
        this.axios.post("/labelTopicChildren/labelTopicChildren/saveOrUpdate",this.childrenFrom).then((res)=> {
          this.childrenFrom = {};
          this.childrenVisible = false;
          this.visible = false;
          this.loadData();
        })
      },
      handleDeleteChildren(record){
          this.axios.get("/labelTopicChildren/labelTopicChildren/delete",{params:{id:record.id}}).then((res) => {
            this.childrenVisible = false;
            this.visible = false;
            this.loadData();
          })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
